export default {
  namespaced: true,
  state: {
    mobile: '',
    password: '',
    avatar: '', //头像
    name: '', // 名称
    user: undefined,
    permissions: null,
    roles: null,
    token: '',
    routesConfig: null,
    expireTime: '',
    farmToken: ''
  },
  getters: {
    user: state => {
      if (!state.user) {
        try {
          const user = localStorage.getItem(process.env.VUE_APP_USER_KEY)
          state.user = JSON.parse(user)
        } catch (e) {
          console.error(e)
        }
      }
      return state.user
    },
    user_mobile: state => {
      const user_mobile = localStorage.getItem('USER_MOBILE')
      state.mobile = user_mobile
      return state.mobile
    },
    password: state => {
      const password = localStorage.getItem('PASSWORD')
      state.password = password
      return state.password
    },
    name: state => {
      const name = localStorage.getItem('NAME')
      state.name = name
      return state.name
    },
    avatar: state => {
      const avatar = localStorage.getItem('AVATAR')
      state.avatar = avatar
      return state.avatar
    },
    token: state => {
      const token = localStorage.getItem('TOKEN')
      state.mobile = token
      return state.token
    },
    farmToken: state => {
      const farmToken = localStorage.getItem('FARMTOKEN')
      state.farmToken = farmToken
      return state.farmToken
    },
    expire_time: state => {
      const expire_time = localStorage.getItem('EXPIRE_TIME')
      state.expireTime = expire_time
      return state.expireTime
    },
    permissions: state => {
      if (!state.permissions) {
        try {
          const permissions = localStorage.getItem(process.env.VUE_APP_PERMISSIONS_KEY)
          state.permissions = JSON.parse(permissions)
          state.permissions = state.permissions ? state.permissions : []
        } catch (e) {
          console.error(e.message)
        }
      }
      return state.permissions
    },
    roles: state => {
      if (!state.roles) {
        try {
          const roles = localStorage.getItem(process.env.VUE_APP_ROLES_KEY)
          state.roles = JSON.parse(roles)
          state.roles = state.roles ? state.roles : []
        } catch (e) {
          console.error(e.message)
        }
      }
      return state.roles
    },
    routesConfig: state => {
      if (!state.routesConfig) {
        try {
          const routesConfig = localStorage.getItem(process.env.VUE_APP_ROUTES_KEY)
          state.routesConfig = JSON.parse(routesConfig)
          state.routesConfig = state.routesConfig ? state.routesConfig : []
        } catch (e) {
          console.error(e.message)
        }
      }
      return state.routesConfig
    }
  },
  mutations: {
    setUser (state, user) {
      state.user = user
      localStorage.setItem(process.env.VUE_APP_USER_KEY, JSON.stringify(user))
    },
    setMobile (state, mobile) {
      state.mobile = mobile
      localStorage.setItem('USER_MOBILE', mobile)
    },
    setName (state, name) {
      state.name = name
      localStorage.setItem('NAME', name)
    },
    setAvatar (state, avatar) {
      state.avatar = avatar
      localStorage.setItem('AVATAR', avatar)
    },
    setPassword (state, password) {
      state.password = password
      localStorage.setItem('PASSWORD', password)
    },
    setExpireTime(state, expireTime) {
      state.expireTime = expireTime
      localStorage.setItem('EXPIRE_TIME', expireTime + 3000)
    },
    setToken(state, token) {
      state.token = token
      localStorage.setItem('TOKEN', token)
    },
    setFarmToken(state, farmToken) {
      state.farmToken = farmToken
      localStorage.setItem('FARMTOKEN', farmToken)
    },
    setPermissions(state, permissions) {
      state.permissions = permissions
      localStorage.setItem(process.env.VUE_APP_PERMISSIONS_KEY, JSON.stringify(permissions))
    },
    setRoles(state, roles) {
      state.roles = roles
      localStorage.setItem(process.env.VUE_APP_ROLES_KEY, JSON.stringify(roles))
    },
    setRoutesConfig(state, routesConfig) {
      state.routesConfig = routesConfig
      localStorage.setItem(process.env.VUE_APP_ROUTES_KEY, JSON.stringify(routesConfig))
    }
  }
}
